package com.kfm.jdbc.day0405;

public class Demo {

    public static void main(String[] args) {
        PrintLetterThread t1 = new PrintLetterThread('A', 50, null);
        PrintLetterThread t2 = new PrintLetterThread('B', 50, t1);
        PrintLetterThread t3 = new PrintLetterThread('C', 50, t2);

//        PrintNumberRunnable r = new PrintNumberRunnable();
//
//        Thread t1 = new Thread(r, "A");
//        Thread t2 = new Thread(r, "B");
//        Thread t3 = new Thread(r, "C");

        t1.start();
        t2.start();
        t3.start();

        // main 线程

    }
}

class PrintLetterThread extends Thread{

    private char letter;
    private int times;
    private Thread wait;

    public PrintLetterThread(char letter, int times, Thread wait) {
        this.letter = letter;
        this.times = times;
        this.wait = wait;
    }

    @Override
    public void run() {
        if (wait != null){
            try {
                // t1 执行时，wait == null，不等待
                // t2 执行时，t1.join()，t2 等待 t1执行完毕
                // t3 执行时，t2.join()，t3 等待 t2执行完毕
                wait.join();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        for (int i = 0; i < times; i++) {
            System.out.print(letter);
        }
    }
}

class PrintNumberRunnable implements Runnable{

    @Override
    public void run() {
        String name = Thread.currentThread().getName();
        for (int i = 0; i < 3; i++) {
            System.out.print(name);
        }
    }
}
